<title>账号管理</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>系统管理</cite></a>
        <a><cite>账号管理</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="请输入关键词" class="layui-input">
                </div>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" lay-submit lay-filter="LAY-system-user-search"> <i class="layui-icon layui-icon-search"></i> </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-system-user-manage" lay-filter="LAY-system-user-manage"></table>
            <script type="text/html" id="LAY-system-user-toolbar">
                <div class="layui-btn-container">
                    {{# if (AppGeneral.userCan('system.user.destroy')) { }}
                    <btton class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deletes"><i class="layui-icon layui-icon-delete"></i></btton>
                    {{# } }}
                    {{# if (AppGeneral.userCan('system.user.create')) { }}
                    <btton class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></btton>
                    {{# } }}
                </div>
            </script>
        </div>
    </div>
</div>
<script>
    layui.use(['index', 'baseMethods'], function () {
        const {form, layer, table, baseMethods} = layui, tableId = 'LAY-system-user-manage';
        const tableIns = baseMethods.table({
            elem: '#'+tableId,
            url: '/system/user/list',
            toolbar: '#LAY-system-user-toolbar',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'avatar', title: '头像', unresize: true, width: 70,
                    templet: function (data) {
                        return '<img style="border-radius: 50%;" src="' + AppGeneral.storage(data.avatar || "static/admin/img/default_headimg.gif") + '" alt="" width="25" height="25" onerror="AppGeneral.imgError(this)">'
                    }
                },
                {field: 'username', title: '账号'},
                {field: 'display_name', title: '昵称'},
                {field: 'status', title: '状态', unresize: true, width: 100, align: 'center',
                    templet: function (data) {
                        return parseInt(data.status) === 1 ? '<span class="layui-badge layui-bg-green">正常</span>' : '<span class="layui-badge layui-bg-orange">禁用</span>';
                    }
                },
                {field: 'created_at', title: '添加时间', width: 180},
                {field: 'updated_at', title: '修改时间', width: 180},
                {fixed: 'right', width: 180, align: 'center',
                    templet: function () {
                        let html = '<div class="layui-btn-group">\n';
                        if (AppGeneral.userCan('system.user.edit')) {
                            html += '<btton class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></btton>\n';
                        }
                        if (AppGeneral.userCan('system.user.resetPassword')) {
                            html += '<btton class="layui-btn layui-btn-sm layui-btn-normal" lay-event="resetPassword"><i class="layui-icon layui-icon-password"></i></btton>\n';
                        }
                        return html+ '</div>\n';
                    }
                }
            ]]
        });
        let tableTools = {
            add: function () {
                baseMethods.adminLayer([
                    {title: '添加', area: ['750px', '560px'], id: 'LAY-popup-system-user-form'},
                    {tpl: 'system/user/form', id: 'LAY-system-user-form'},
                    {url: '/system/user/store', type: "post",
                        success: function () {
                        console.log('asdasdasdsa')
                            tableIns.reloadData();
                        }
                    },
                ]);
            },
            deletes: function (obj) {
                const hasCheck = table.checkStatus(obj.config.id), hasCheckData = hasCheck.data;
                if (hasCheckData.length > 0){
                    let ids = hasCheckData.map(item => item.id);
                    if (AppGeneral.inArray(1, ids)) return layer.msg('初始管理员不能删除', {icon: 5});
                    baseMethods.toolOpera({
                        title: `确认删除选中的 ${hasCheckData.length} 项吗？`,
                        request: {
                            url: '/system/user/destroy',
                            type: "delete",
                            data: {ids},
                            success: function () {
                                tableIns.reloadData();
                            }
                        },
                    })
                    return true;
                }
                layer.msg('请选择操作项', {icon:5});
            },
            edit: function (obj) {
                let data = obj.data;
                baseMethods.adminLayer([
                    {title: '编辑', area: ['750px', '560px'], id: 'LAY-popup-system-user-form'},
                    {tpl: "system/user/form", mode: 'edit', data, id: 'LAY-system-user-form'},
                    {url: '/system/user/'+data.id+'/update', type: "put",
                        success: function (field){
                            obj.update(field);
                        }
                    },
                ])
            },
            resetPassword: function (obj) {
                let data = obj.data;
                layer.prompt({ formType: 1, title: '重置密码', area: ['45%', 'auto'], shadeClose: true, }, function(value, index, elem) {
                    if (!value) return layer.msg('请输入您的密码', {icon: 5});
                    if(!new RegExp(/^[\S]{8,16}$/).test(value)) return layer.msg('密码必须8到16位，且不能出现空格', {icon: 5});
                    baseMethods.request({
                        url: '/system/user/'+ data.id +'/resetPassword',
                        type: "put",
                        data: {password: value},
                        done: function (){
                            layer.close(index);
                        }
                    })
                });
            }
        };
        table.on('toolbar('+tableId+')', function(obj) {
            tableTools[obj.event] ? tableTools[obj.event].call(this, obj) : '';
        });
        table.on('tool('+tableId+')', function (obj) {
            tableTools[obj.event] ? tableTools[obj.event].call(this, obj) : '';
        });
        form.on('submit(LAY-system-user-search)', function(data){
            tableIns.reloadData({
                where: data.field,
                page: {curr:1}
            })
        });
    });
</script>
